import http from '../http/http.js';
import qs from "qs";
import {
	getMD5Sign,
	getRandomText
} from "../utils/util.js";

export default {
	login() {
		wx.login({
			complete: res => {
				if (res.errMsg === 'login:ok') {
					http({
						method: "post",
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						data: qs.stringify({
							js_code: res.code
						}),
						url: "/auth/mini-program/login",
					}).then((res) => {
						console.log(res);
						if (res.code === 200) {

						}
					}).catch((e) => {
						return e;
					});
				} else {
					this.login()
				}
			}
		})
	},
	getUserInfo() {
		return http({
			url: "/api/user/info",
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	productQuery(productId) {
		return http({
			url: "/product/query",
			params: {
				productId
			},
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	cartUpdate({
		ids,
		productId,
		count
	} = {}) {
		let data = {
			productId,
			count,
		}
		if (ids) {
			data.ids = ids;
		}
		data.random = getRandomText();
		data.sign = getMD5Sign(data, true);
		return http({
			url: "/api/cart/update",
			data,
			method: 'post',
		}).catch((e) => {
			return e
		});
	},
	cartQuery() {
		return http({
			url: "/api/cart/query",
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	cartDelete({
		ids
	}) {
		let data = {
			ids
		}
		data.random = getRandomText();
		data.sign = getMD5Sign(data, true);
		return http({
			url: "/api/cart/delete",
			method: 'post',
			data
		}).catch((e) => {
			return e
		});
	},
	getQrcode: function (productId) {
		return http({
			url: "/product/qrcode",
			method: 'get',
			data: {
				productId: productId
			}
		}).catch((e) => {
			return e
		});
	},
	getShareTime: function (productId) {
		return http({
			url: "/share/query",
			method: 'get',
			data: {
				productId: productId
			}
		}).catch((e) => {
			return e
		});
	},
	setSharetime: function (productId) {
		return http({
			url: "/share/share",
			method: 'get',
			data: {
				productId: productId
			}
		}).catch((e) => {
			return e
		});
	},
	setShareHotelTime: function (hotelId) {
		return http({
			url: "/share/shareHotel",
			method: 'get',
			data: {
				hotelId: hotelId
			}
		}).catch((e) => {
			return e
		});
	},
	getOrders: function (current, size, orderStatus) {
		const params = {
			current,
			size,
			orderStatus
		}
		// 如果params中任意值为undefined则删除该键值对
		Object.keys(params).forEach(key => {
			if (params[key] === undefined) {
				delete params[key];
			}
		});
		return http({
			url: "/api/order",
			method: 'get',
			params
		}).catch((e) => {
			return e
		});
	},
	orderModuleDetails: function (status) {
		return http({
			url: "/api/order/module/details",
			method: 'get',
			params: {
				status
			}
		}).catch((e) => {
			return e
		});
	},
	getOrdersByStatus: function (status) {
		return http({
			url: "/api/order/module/details",
			method: 'get',
			params: {
				status
			}
		}).catch((e) => {
			return e
		});
	},
	addressQuery: function () {
		return http({
			url: "/api/user/query/address",
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	updateAddress: function ({ receiverId, receiverName, receiverTel, receiverProvince, receiverCity, receiverArea, receiverAddress, defaultType } = {}) {
		return http({
			url: "/api/user/update/address",
			data: { receiverId, receiverName, receiverTel, receiverProvince, receiverCity, receiverArea, receiverAddress, defaultType },
			method: 'post',
		}).catch((e) => {
			return e
		});
	},
	deleteAddress: function ({ receiverId } = {}) {
		return http({
			url: "/api/user/delete/address",
			params: { receiverId },
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	cityQuery: function ({ level, preCode } = {}) {
		return http({
			url: "/city/query",
			method: 'get',
			params: { level, preCode }
		}).catch((e) => {
			return e
		});
	},
	productSearch: function ({ content, pageNo, pageSize } = {}) {
		return http({
			url: "/product/search",
			method: 'get',
			params: { content, pageNo, pageSize }
		}).catch((e) => {
			return e
		});
	},
	upDataUserInfo: function ({ nickname, avatar, sex } = {}) {
		return http({
			url: "/api/user",
			method: 'put',
			data: { nickname, avatar, sex }
		}).catch((e) => {
			return e
		});
	},
	getProductByTag: function ({ tag, pageNo, pageSize } = {}) {
		return http({
			url: "/product/tag",
			method: 'get',
			data: { tag, pageNo, pageSize }
		}).catch((e) => {
			return e
		});
	},
	couponConfig({ source } = {}) {
		return http({
			url: `/api/coupon/list`,
			params: { source },
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	couponReceived({ isVirtual } = {}) {
		return http({
			url: `/api/coupon/received/list`,
			params: { isVirtual },
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	couponReceiveAction({ couponId } = {}) {
		return http({
			url: `/api/coupon/receive`,
			params: { couponId },
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	getProductSeries({ product } = {}) {
		return http({
			url: `/product/series`,
			params: { product },
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	getProductModuleList() {
		return http({
			url: `/product/module/list`,
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
	getProductModule({ moduleId, pageNo, pageSize } = {}) {
		return http({
			url: `/product/module`,
			params: { moduleId, pageNo, pageSize },
			method: 'get',
		}).catch((e) => {
			return e
		});
	},
};
